package com.furongji.common.utils;

import com.furongji.common.exception.MyException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

public class JwtUtil {

    private static String sign = "aWtH5qJzL9X2v8R1yC3nD7mF4bK6pE9sA0xW5gT7hY2jU=";

    public static String createToken(String userId, String username) {
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, sign)
                .claim("userId", userId)
                .claim("username", username)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7L))
                .compact();
        return token;
    }

    public static Claims parseToken(String token) {
        if (token == null) {
            throw new MyException(20001, "未登录");
        }
        Claims body = Jwts.parser().setSigningKey(sign).parseClaimsJws(token).getBody();
        return body;
    }

//    public static void main(String[] args) {
//        System.out.println(createToken(123456L, "furongji"));
//        System.out.println(parseToken("eyJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEyMzQ1NiwidXNlcm5hbWUiOiJmdXJvbmdqaSIsImV4cCI6MTc3OTcwNTk5M30.DaVVryb7UqSQ6qNU5tpf4mY-eYR8a5U1H2khRpMj8uU"));
//    }
}
